package com.zensun.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zensun.common.constant.UserConstants;
import com.zensun.common.utils.Assert;
import com.zensun.system.domain.SysLogininfor;
import com.zensun.system.domain.dto.SysLogininforDto;
import com.zensun.system.mapper.SysLogininforMapper;
import com.zensun.system.service.ISysLogininforService;
import com.zensun.system.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author gmk
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor>
        implements ISysLogininforService {

    @Autowired
    private SysLogininforMapper logininforMapper;

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        logininfor.setLoginTime(new Date());
        logininforMapper.insert(logininfor);
    }


    @Override
    public List<SysLogininfor> list(SysLogininforDto logininforDto) {
        return logininforMapper.selectLogininforList(logininforDto);
    }

    @Override
    public List<SysLogininfor> listOneSelf(SysLogininforDto logininforDto) {
        String userName = SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUser().getUserId()) ? UserConstants.ADMIN_USENAME : SecurityUtils.getUsername();
        logininforDto.setUserName(userName);
        return logininforMapper.selectLogininforList(logininforDto);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return
     */
    @Override
    public boolean deleteLogininforByIds(Long[] infoIds) {
        Assert.isNotTrue(infoIds != null && infoIds.length > 0, "删除的日志不存在，请确认后再试");
        List<Long> list = new ArrayList<Long>();
        for (Long row : infoIds) {
            list.add(row);
        }
        return this.removeByIds(list);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        logininforMapper.cleanLogininfor();
    }
}
